import { OrganizationConst } from "src/consts/organization_const";
import * as Api from 'src/api/net';
import { PopupName } from "src/consts/popup_const";

const state: ListMsgData = {} as any;
// StateIniter.init(state, 'demo');

state.curr_page = 1;
state.page_size = 15;
state.total_page = 1;
state.total_size = 1;
state.data_list = [];
state.type = "";
// vuex

/** 更新数据的 */
const mutations = {
    [OrganizationConst.CommitOrgAcc](state: ListMsgData, data: ListMsgData): void {
        console.log("org_acc data update", data);
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: DemoData = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;
        WLLib.panel(PopupName.Org_Acc_Panel);
    },
    [OrganizationConst.CommitType](state: ListMsgData, data: String): void {
        state.type = data
    }
}

//异步处理
const actions = {
    [OrganizationConst.DispatchOrgAcc](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/account/list.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                var list = []
                if (isNoEmpty(data.select)) {
                    for (let i = 0; i < res.data.data_list.length; i++) {
                        var flag = true
                        for (let j = 0; j < data.select.length; j++) {
                            if (res.data.data_list[i].id == data.select[j].id) {
                                flag = false
                            }
                        }

                        if (flag) {
                            list.push(res.data.data_list[i])
                        }
                    }
                    res.data.data_list = list
                }

                WLCommit(OrganizationConst.CommitOrgAcc, res.data);
            }
        })
    },
}


export default {
    state,
    mutations,
    actions
}